package com.xxxx.portal.controller;

import com.github.qcloudsms.httpclient.HTTPException;
import com.xxxx.common.pojo.Admin;
import com.xxxx.common.result.RespBean;
import com.xxxx.common.utils.TCaptchaVerify;
import com.xxxx.portal.service.AdminService;
import com.xxxx.portal.service.CookieService;
import com.xxxx.sso.service.SSOService;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 用户
 *
 * @Author GP
 * @Date 2020/8/31 19:59
 */
@Controller
public class AdminController {
    @Resource
    private CookieService cookieService;

    @Reference(interfaceClass = SSOService.class)
    private SSOService ssoService;

    @Resource
    private AdminService adminService;


    /**
     * 默认跳转页面
     */
    @RequestMapping("/")
    public String init(HttpServletRequest request, HttpServletResponse response) {
        return "index";
    }


    /**
     * 单点登录功能
     */
    @ResponseBody
    @RequestMapping("/user/login")
    public RespBean login(HttpServletRequest request, HttpServletResponse response, Admin admin) {

        //登录，返回ticket票据
        String ticket = ssoService.login(admin);
        //判断ticket是否为空
        if (StringUtils.isEmpty(ticket)) {
            return RespBean.error("ticket票据为空，无法登录！");
        }
        //将ticket写入cookie
        Boolean cookie = cookieService.setCookie(request, response, ticket);
        System.out.println(cookie);
        if (cookie) {
            //将用户信息存入session中，以便后续操作
            request.getSession().setAttribute("user", admin);
            return RespBean.success("登录成功！");
        }
        return RespBean.error("登录失败");
    }

    /**
     * 退出登录
     * 销毁redis,销毁ticket
     * 销毁session,销毁用户信息
     * 销毁cookie，销毁ticket
     */
    @RequestMapping("/user/logout")
    public String logout(HttpServletRequest request, HttpServletResponse response) {
        //获得ticket
        String ticket = cookieService.getCookie(request);
        //清除Redis中ticket
        ssoService.logout(ticket);
        //删除session,销毁用户信息
        request.getSession().removeAttribute("user");
        //删除cookie,销毁ticket
        cookieService.deleteCookie(request, response);
        return "login";
    }

    /**
     * 验证码
     */
    @RequestMapping("/user/captcha")
    @ResponseBody
    public int captcha(String ticket, String rand) {
        return TCaptchaVerify.verifyTicket(ticket, rand);
    }


    /**
     * 用户注册
     */
    @RequestMapping("/user/register")
    @ResponseBody
    public RespBean register(Admin admin) throws HTTPException, IOException {
        Boolean flag = adminService.register(admin);
        return flag ? RespBean.success("注册成功！") : RespBean.error("注册失败！");
    }
}
